//
//  FFCache.m
//  fitness-iOS
//
//  Created by 卢仕彤 on 2017/12/14.
//  Copyright © 2017年 卢仕彤. All rights reserved.
//

#import "FFCache.h"

@implementation FFCache

+ (void)setequipment:(NSString *)equipment {
    equipment = [NSString stringWithFormat:@"%@", equipment];
    [[NSUserDefaults standardUserDefaults] setObject:equipment forKey:EQUIPMENT];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (NSString *)equipment {
    if (![[NSUserDefaults standardUserDefaults] objectForKey:EQUIPMENT]) {
        return @"";
    }
    return [[NSUserDefaults standardUserDefaults] objectForKey:EQUIPMENT];
}

+ (void)setToken:(NSString *)token {
    token = [NSString stringWithFormat:@"%@", token];
    [[NSUserDefaults standardUserDefaults] setObject:token forKey:TOKEN];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (NSString *)token {
    if (![[NSUserDefaults standardUserDefaults] objectForKey:TOKEN]) {
        return @"";
    }
    return [[NSUserDefaults standardUserDefaults] objectForKey:TOKEN];
}

+ (void)clearCache {
    NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
    NSDictionary* dict = [defs dictionaryRepresentation];
    NSArray * cache_keys = [[NSArray alloc] initWithArray:[dict allKeys]];
    for (int i = 0; i < [cache_keys count]; i++) {
        if ([((NSString *)cache_keys[i]) containsString:@"ff"]) {
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:cache_keys[i]];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
    }
}


+ (void)setIslogin:(NSString *)login {
    login = [NSString stringWithFormat:@"%@", login];
    [[NSUserDefaults standardUserDefaults] setObject:login forKey:ISLOGIN];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (BOOL)isLogin {
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:ISLOGIN] length] > 0) {
        return YES;
    }
    else {
        return NO;
    }
}

@end
